package com.yanqu.road.dao.impl.activity.horserun.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.cross.CrossHorseRunUserData;
import com.yanqu.road.entity.activity.horserun.cross.HorseRunRacingUserResult;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHorseRunUserDataDaoImpl extends TempDataObjectDao<CrossHorseRunUserData> {
    public static String getCrossHorseRunUserDataSql = "select * from `t_c_horse_run_user_data` where `activityId`=? and `groupId`=?";
    public Map<Long, CrossHorseRunUserData> getCrossHorseRunUserData(int activityId,int groupId) {

        Map<Long, CrossHorseRunUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossHorseRunUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossHorseRunUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossHorseRunUserDataSql = "update `t_c_horse_run_user_data` set unionId=?,horseAppearance=?,equipment=?,level=?,nowGrade=?,maxGrade=?,skillList=?,attrLevelMap=?,extAttr = ?,totalAttr=?,signData=?,racingResult=?,cacheExp=?,lastGetExpTime=?,unreadRound=?,lastSignTime=? where activityId=? and groupId=? and userId=?";
    @Override
    protected boolean updateData(CrossHorseRunUserData crossHorseRunUserData) {
        crossHorseRunUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getHorseAppearance()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getEquipment())));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getNowGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getMaxGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getAttrLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getExtAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getTotalAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getSignData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getRacingResult())));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getCacheExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getLastGetExpTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getUnreadRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getLastSignTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossHorseRunUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossHorseRunUserDataSql = "insert into `t_c_horse_run_user_data`(activityId,groupId,userId,unionId,horseAppearance,equipment,level,nowGrade,maxGrade,skillList,attrLevelMap,extAttr,totalAttr,signData,racingResult,cacheExp,lastGetExpTime,unreadRound,lastSignTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossHorseRunUserData crossHorseRunUserData) {
        crossHorseRunUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunUserData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getHorseAppearance()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getEquipment())));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getNowGrade()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getMaxGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getSkillList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getAttrLevelMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getExtAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getTotalAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getSignData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUserData.getRacingResult())));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getCacheExp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getLastGetExpTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUserData.getUnreadRound()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUserData.getLastSignTime()));
        if (getDb().executeNoneQuery(insertCrossHorseRunUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossHorseRunUserData parseData(ResultSet rs) throws SQLException {
        CrossHorseRunUserData crossHorseRunUserData = new CrossHorseRunUserData();
        crossHorseRunUserData.setActivityId(rs.getInt("activityId"));
        crossHorseRunUserData.setGroupId(rs.getInt("groupId"));
        crossHorseRunUserData.setUserId(rs.getLong("userId"));
        crossHorseRunUserData.setUnionId(rs.getString("unionId"));
        crossHorseRunUserData.setHorseAppearance(rs.getInt("horseAppearance"));
        crossHorseRunUserData.setEquipment(JSON.parseObject(rs.getString("equipment"), new TypeReference<List<Integer>>() {}));
        crossHorseRunUserData.setLevel(rs.getInt("level"));
        crossHorseRunUserData.setNowGrade(rs.getLong("nowGrade"));
        crossHorseRunUserData.setMaxGrade(rs.getLong("maxGrade"));
        crossHorseRunUserData.setSkillList(JSON.parseObject(rs.getString("skillList"), new TypeReference<List<Integer>>() {}));
        crossHorseRunUserData.setAttrLevelMap(JSON.parseObject(rs.getString("attrLevelMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunUserData.setExtAttr(JSON.parseObject(rs.getString("extAttr"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunUserData.setTotalAttr(JSON.parseObject(rs.getString("totalAttr"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunUserData.setSignData(JSON.parseObject(rs.getString("signData"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunUserData.setRacingResult(JSON.parseObject(rs.getString("racingResult"), new TypeReference<ConcurrentHashMap<Integer, HorseRunRacingUserResult>>() {}));
        crossHorseRunUserData.setCacheExp(rs.getLong("cacheExp"));
        crossHorseRunUserData.setLastGetExpTime(rs.getLong("lastGetExpTime"));
        crossHorseRunUserData.setUnreadRound(rs.getInt("unreadRound"));
        crossHorseRunUserData.setLastSignTime(rs.getLong("lastSignTime"));

        return crossHorseRunUserData;
    }

}
